﻿using System.Collections.Generic;
using System.Linq;
using Nest;

namespace Infrastructure.ElasticSearch.ParentChilds
{
    public class EsMultiSearchMustBuilder<T> : ISearchSpecification<ISearchRequest<T>> where T : class
    {
        private readonly List<ISearchSpecification<ISearchRequest<T>>> _specifications;

        public EsMultiSearchMustBuilder(List<ISearchSpecification<ISearchRequest<T>>> specifications)
        {
            _specifications = specifications;
        }

        public bool IsNest { get; set; } = false;

        public ISearchRequest<T> IsSatisfiedBy()
        {
            return new SearchDescriptor<T>()
                .Query(q => q
                    .Bool(b => b
                        .Must(
                            _specifications.Select(x => x
                                .IsSatisfiedBy().Query
                            ).ToArray()
                        )
                    )
                );
        }
    }
}